﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExplorerPlus.Controls
{
    /// <summary>
    /// This class is an implementation of the 'IComparer' interface.
    /// </summary>
    public class ListView : System.Windows.Forms.ListView
    {
        private ListViewColumnSorter lvwColumnSorter;

        public ListView()
        {
            // Create an instance of a ListView column sorter and assign it 
            // to the ListView control.
            this.View = System.Windows.Forms.View.Details;
            lvwColumnSorter = new ListViewColumnSorter();
            this.ListViewItemSorter = lvwColumnSorter;
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ListView
            // 
            this.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.ListView_ColumnClick);
            this.ResumeLayout(false);

        }

        private void ListView_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (lvwColumnSorter.Order == System.Windows.Forms.SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;
            }

            // Perform the sort with these new sort options.
            this.Sort();
        }

        /// <summary>
        /// Size each column header to fit the column header text.
        /// </summary>
        public void ResizeColumns()
        {
            // Loop through and size each column header to fit the column header text.
            foreach (System.Windows.Forms.ColumnHeader ch in this.Columns)
            {
                ch.Width = -2;
            }
        }

        /// <summary>
        /// Reset sort column and sort order to none.
        /// </summary>
        public void ResetSort()
        {
            this.lvwColumnSorter.Order = System.Windows.Forms.SortOrder.None;
            this.lvwColumnSorter.SortColumn = 0;
        }

    }
}
